package com.study_online.service.Impl;/*
    张昊（java文件）
 
*/


import com.study_online.domain.*;
import com.study_online.interceptor.LoginInterceptor;
import com.study_online.mapper.CommentMapper;
import com.study_online.service.CommentService;
import com.study_online.utils.JjwtUtil;
import com.study_online.utils.TokenUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;


@Service
public class CommentServiceImpl implements CommentService {



    @Autowired
    private CommentMapper commentMapper;


    //根据视频的id查询该视频下的所有评论
    @Override
    public ResultInfo findById(int vido_id) {
        List<Comment> lc=new ArrayList<>();
        Comment comment=new Comment();
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            //获取评论信息以及该评论下的所有回复
            //获取评论
            List<CommentB> list = commentMapper.findById(vido_id);
            //根据获取到的评论查看回复信息
            for (CommentB l : list) {
                List<CommentBelonging> commentBelonging = commentMapper.findCommentBelonging(l.getCommentId());
                comment.setCommentId(l.getCommentId());
                comment.setVidoId(l.getVidoId());
                comment.setUserId(l.getUserId());
                comment.setText(l.getText());
                comment.setUpdataTime(l.getUpdataTime());
                comment.setAddTime(l.getAddTime());
                //判断是否有回复信息
                if (commentBelonging==null){
                    commentBelonging=null;
                }
                comment.setCommentBelonging(commentBelonging);
                lc.add(comment);
            }
            //List<CommentB> list = commentMapper.findById(vido_id);
            info.setData(lc);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查看失败");
            e.printStackTrace();
        }
        return info;
    }

    //添加评论信息
    @Override
    public ResultInfo addComment(int vido_id, String text,String addtime) {
        Integer uid = LoginInterceptor.getUser().getUid();
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            int i = commentMapper.addComment(vido_id, uid, text, addtime);
            info.setData(i);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("添加失败");
            e.printStackTrace();
        }
        return info;
    }

    //删除评论信息
    @Override
    public ResultInfo deleteComment(int commentId) {
        Integer uid = LoginInterceptor.getUser().getUid();
        ResultInfo info=new ResultInfo();
        info.setFlag(true);
        try {
            commentMapper.deleteCommentB(commentId);
            int i = commentMapper.deleteComment(commentId,uid);
            info.setData(i);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("删除失败");
            e.printStackTrace();
        }
        return info;
    }

    //添加评论的回复
    @Override
    public ResultInfo addCommentB(int commentBId,String text, String addtime) {
        Integer uid = LoginInterceptor.getUser().getUid();
        ResultInfo info=new ResultInfo();
        info.setFlag(true);
        try {

            int i=commentMapper.addCommentB(commentBId,uid,text,addtime);
            info.setData(i);
        }catch (Exception e){
            info.setFlag(false);
            info.setErrorInfo("添加失败");
            e.printStackTrace();
        }
        return info;
    }

    //删除根据id删除回复的内容
    @Override
    public ResultInfo deleteCommentB(int commentBId) {
        Integer uid = LoginInterceptor.getUser().getUid();
        ResultInfo info=new ResultInfo();
        info.setFlag(true);
        try {
            int i=commentMapper.deleteCommentBId(commentBId,uid);
            info.setData(i);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("删除失败");
            e.printStackTrace();
        }
        return info;
    }


}
